import type { TableColumn } from '@/components/core/dynamic-table';
import { type TreeDataItem } from '@/core/permission/utils';

export type TableListItem = API.OrgListPageResultItem;
export type TableColumnItem = TableColumn<TableListItem>;

export const baseColumns: TableColumnItem[] = [
  {
    title: '机构名称',
    dataIndex: 'name',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '机构编号',
    dataIndex: 'code',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '上级机构ID',
    dataIndex: 'parentId',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '机构类型',
    dataIndex: 'orgType',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '机构领导',
    dataIndex: 'leader',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '机构领导',
    dataIndex: 'leaderName',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '上级机构领导',
    dataIndex: 'parentLeader',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '上级机构领导',
    dataIndex: 'parentLeaderName',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '联系电话',
    dataIndex: 'phoneNumber',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '是否法人机构',
    dataIndex: 'isLegalPerson',
    align: 'center',
    hideInSearch: true,
    width: 160,
    formItemProps: {
        component: 'Select',
        componentProps: {
          options: [
            {
              label: '是',
              value: 1,
            },
            {
              label: '否',
              value: 0,
            },
          ],
        },
      },
      customRender: ({ record }) => {
        const isEnable = record.isLegalPerson === 1;
        return <Tag color={isEnable ? 'success' : 'red'}>{isEnable ? '是' : '否'}</Tag>;
      },
  },
  {
    title: '营业编码',
    dataIndex: 'operationCode',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '关键字',
    dataIndex: 'keyWords',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '机构网址',
    dataIndex: 'orgNetAddress',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '机构地址',
    dataIndex: 'orgAddress',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '是否启用',
    dataIndex: 'IS_ENABLED',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
    formItemProps: {
        component: 'Select',
        componentProps: {
          options: [
            {
              label: '是',
              value: 1,
            },
            {
              label: '否',
              value: 0,
            },
          ],
        },
      },
      customRender: ({ record }) => {
        const isEnable = record.IS_ENABLED === 1;
        return <Tag color={isEnable ? 'success' : 'red'}>{isEnable ? '是' : '否'}</Tag>;
      },
  },
  {
    title: '是否业务相关',
    dataIndex: 'IS_BUSINESS',
    align: 'center',
    hideInSearch: true,
    width: 160,
    formItemProps: {
        component: 'Select',
        componentProps: {
          options: [
            {
              label: '是',
              value: 1,
            },
            {
              label: '否',
              value: 0,
            },
          ],
        },
      },
      customRender: ({ record }) => {
        const isEnable = record.IS_ENABLED === 0;
        return <Tag color={isEnable ? 'success' : 'red'}>{isEnable ? '是' : '否'}</Tag>;
      },
  },
  {
    title: '机构标签',
    dataIndex: 'orgTags',
    align: 'center',
    hideInSearch: true,
    hideInTable: true,
    width: 160,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
];

export const formatFormSortTree = (
  orgs: API.OrgListPageResultItem[],
  parentNo: number | string = 0,
  keyPath: (string | number)[] = [],
): TreeDataItem[] => {  
  return orgs
    .filter((item) => item.parentId === parentNo)
    .map((item) => {
      const _keyPath = keyPath.concat(parentNo || []);
      const arr = formatFormSortTree(orgs, item.id, _keyPath);
      return Object.assign(item, {
        keyPath: _keyPath,
        title: item.name,
        key: item.id,
        label: item.name,
        value: item.id,
        isLeaf:false,
        children: arr.length ? arr : null,
      });
    });
};
export const formatExpandFormSortTree = (
  orgs: API.OrgListPageResultItem[],
  parentNo: number | string = 0,
  keyPath: (string | number)[] = [],
): TreeDataItem[] => {
  return orgs
    .filter((item) => item.orgId === parentNo||item.parentId === parentNo)
    .map((item) => {
      const _keyPath = keyPath.concat(parentNo || []);
      const arr = formatFormSortTree(orgs, item.id, _keyPath);
      // console.log(arr,item,'arr')
      return Object.assign(item, {
        keyPath: _keyPath,
        title: item.name,
        key: item.id,
        label: item.name,
        value: item.id,
        formData: item,
        isLeaf:false,
        children: arr.length ? arr : null,
      });
    });
};
